﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;


namespace AttrapeMoi
{
    class Pacman : Sprite
    {
        private Texture2D tex_haut;
        private Texture2D tex_haut_ferme;
        private Texture2D tex_bas;
        private Texture2D tex_bas_ferme;
        private Texture2D tex_gauche;
        private Texture2D tex_gauche_ferme;
        private Texture2D tex_droite;
        private Texture2D tex_droite_ferme;
        private Texture2D texture_ferme;
        bool est_normal;

        public override void Initialize(string assetName)
        {
            base.Initialize(assetName);
            speed = 0.125F;
            est_normal = true;
        }

        /// Charge l'image voulue grâce au ContentManager donné
        /// </summary>
        /// <param name="content">Le ContentManager qui chargera l'image</param>
        /// <param name="largeur">La largeur de l'ecran</param>
        /// <param name="hauteur">La hauteur de l'ecran</param>
        public override void LoadContent(ContentManager content)
        {
            texture = content.Load<Texture2D>(nom);
            texture_ferme = content.Load<Texture2D>(nom + "_ferme");
            tex_bas = content.Load<Texture2D>(nom + "_4");
            tex_gauche = content.Load<Texture2D>(nom + "_2");
            tex_droite = content.Load<Texture2D>(nom);
            tex_haut = content.Load<Texture2D>(nom + "_3");
            tex_bas_ferme = content.Load<Texture2D>(nom + "_4_ferme");
            tex_gauche_ferme = content.Load<Texture2D>(nom + "_2_ferme");
            tex_droite_ferme = content.Load<Texture2D>(nom + "_ferme");
            tex_haut_ferme = content.Load<Texture2D>(nom + "_3_ferme");
            //position = new Vector2(largeur / 2 - texture.Width / 2, hauteur / 2 - texture.Height / 2);
        }

        /// <summary>
        /// Met à jour les variables du sprite
        /// </summary>
        /// <param name="gameTime">Le GameTime associé à la frame</param>
        public override void Update(GameTime gameTime, int longueur_x, int longueur_y)
        {
            position += direction * speed /* * (float)gameTime.ElapsedGameTime.TotalMilliseconds*/;

            if (position.X >= 45 && direction == new Vector2(1, 0))
                direction = new Vector2(0, 1);
            else if (position.Y >= 16 && direction == new Vector2(0, 1))
                direction = new Vector2(-1, 0);
            else if (position.X <= 15 && direction == new Vector2(-1, 0))
                direction = new Vector2(0, -1);
            else if (position.Y <= 13 && direction == new Vector2(0, -1))
                direction = new Vector2(1, 0);

            if (est_normal)
            {
                if (direction == new Vector2(1, 0))
                {
                    texture = tex_droite;
                }
                else if (direction == new Vector2(-1, 0))
                {
                    texture = tex_gauche;
                }
                else if (direction == new Vector2(0, 1))
                {
                    texture = tex_bas;
                }
                else if (direction == new Vector2(0, -1))
                {
                    texture = tex_haut;
                }

                //est_normal = false;
            }
            else
            {
                if (direction == new Vector2(1, 0))
                {
                    texture = tex_droite_ferme;
                }
                else if (direction == new Vector2(-1, 0))
                {
                    texture = tex_gauche_ferme;
                }
                else if (direction == new Vector2(0, 1))
                {
                    texture = tex_bas_ferme;
                }
                else if (direction == new Vector2(0, -1))
                {
                    texture = tex_haut_ferme;
                }
                //est_normal = true;
            }
        }

        public virtual void changeTex(object sender, EventArgs e)
        {
            est_normal = !est_normal;
        }

    }
}